﻿using System.Collections.Specialized;

namespace VitCode.Castle.Helper
{
    /// <summary>
    /// 字元為Key Object為Value的集合,
    /// 這個集合避免了Key是String型的時候，使用Hashtable進行大量UnBox和INbox操作而創作，
    /// 對於資料量少於1000以下的，可以使用Hashtable，因為性能並不太明顯。
    /// </summary>
    /// <remarks></remarks>
    public class NameObjectDictionary : NameObjectCollectionBase
    {
        /// <summary>
        /// 
        /// </summary>
        public NameObjectDictionary()
        {
        }

        /// <summary>
        /// 加入物件
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="value">值</param>
        public virtual void Add(string key, object value)
        {
            this.BaseAdd(key, value);
        }

        /// <summary>
        /// 移除Key和關聯的物件
        /// </summary>
        /// <param name="key"></param>
        public virtual void Remove(string key)
        {
            this.BaseRemove(key);
        }

        /// <summary>
        /// 清除集合內的物件
        /// </summary>
        public virtual void Clear()
        {
            this.BaseClear();
        }

        /// <summary>
        /// 獲取所有Key
        /// </summary>
        public virtual string[] AllKeys
        {
            get { return base.BaseGetAllKeys(); }
        }

        /// <summary>
        /// 獲取是否包換這個Key的物件
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public virtual bool Contains(string key)
        {
            string[] keys = AllKeys;
            for (int i = 0; i < keys.Length; i++)
            {
                if (keys[i] == key)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 獲取或設置這個集合Key所關聯的物件
        /// </summary>
        public virtual object this[string key]
        {
            get { return base.BaseGet(key); }
            set { BaseSet(key, value); }
        }

        /// <summary>
        /// 獲取這個集合所帶的Value
        /// </summary>
        public virtual object[] Values
        {
            get { return BaseGetAllValues(); }
        }
    }
}
